<?php

include('../includes/smarty.php');
include_once('../config.php');

$ADMIN = new Admin();
$EMPLOYER = new Employer();
$INDUSTRY = new Industry();
$EXPERIENCE = new Experience();
$JOBS = new Jobs();

session_start();

if(isset($_SESSION["id"]))
{
	//	Validate the session.
	$row = $ADMIN->validate_admin_session($_SESSION["id"]);
	
	if($row)
	{		
		//	Show the screen.
		$ADMIN->update_lastvisit($_SESSION["id"]);
		
		//	Get the existing companies.
		$companies = $EMPLOYER->get_companies();
		
		if(empty($_POST))
		{
			$smarty->assign('states', 		get_state_select());
			$smarty->assign('industry', 	$INDUSTRY->get_industry_select());
			$smarty->assign('experience', 	$EXPERIENCE->get_experience_select());
			$smarty->assign('zipREX', 		$regex_zip);
			$smarty->assign('phoneREX', 	$regex_phone);
			$smarty->assign('emailREX', 	$regex_email);	
			$smarty->assign('title', 		'Admin: Post a new Job');
			$smarty->assign('path', 		$site);
			$smarty->assign('body', 		'main/admin/post.tpl');
			$smarty->assign('companylist', 	$companies);
			$smarty->assign('message',		'');

			$smarty->display('admin.tpl');
		}
		else
		{
			//	Posting a new submission.	
			
			$companyName	 	= $_POST["companyName"];	//posts company name
			$jobTitle 			= $_POST["jobtitle"];		//posts job title
			$address 			= $_POST["streetaddress"];	//posts address
			$city 				= $_POST["city"];			//posts city
			$state 				= $_POST["state"];			//posts state
			$zip 				= $_POST["zip"];			//posts zip
			$oDate 				= $_POST["odate"];			//posts opening date
			$cDate 				= $_POST["cdate"];			//posts closing date
			$wage 				= $_POST["wage"];			//posts starting wage
			$time 				= $_POST["time"];			//posts full time
			$dur 				= $_POST["dur"];			//posts permanent position
			$shift 				= $_POST["shift"];			//posts the job shift
			$trans			 	= $_POST["trans"];			//posts if transportation is available
			$expRequired 		= $_POST["expRequired"];	//posts experience required
			$expLevel 			= $_POST["expLevel"];		//posts experience level required
			$jobDesc 			= $_POST["jobdesc"];		//posts the job description
			$reqQual 			= $_POST["qualifications"];	//posts the requirements/qualifications needed for job
			$howApply 			= $_POST["howtoapply"];		//posts how to apply for job
			$otherNotes 		= $_POST["othernotes"];		//posts other notes for job
			
			$JOBS->add_post($companyName, $jobTitle, $address, $city, $state, $zip, $oDate, $cDate, $wage, $time, $dur, $shift, $trans, $expRequired, $expLevel, $jobDesc, $reqQual, $howApply, $otherNotes);
			
			//	Success
			
			$smarty->assign('states', 		get_state_select());
			$smarty->assign('industry', 	$INDUSTRY->get_industry_select());
			$smarty->assign('experience', 	$EXPERIENCE->get_experience_select());
			$smarty->assign('zipREX', 		$regex_zip);
			$smarty->assign('phoneREX', 	$regex_phone);
			$smarty->assign('emailREX', 	$regex_email);	
			$smarty->assign('title', 		'Admin: Post a new Job');
			$smarty->assign('path', 		$site);
			$smarty->assign('body', 		'main/admin/post.tpl');
			$smarty->assign('companylist', 	$companies);
			$smarty->assign('message',		'Successfully posted the new job.');

			$smarty->display('admin.tpl');
		}
	}
	else
	{
		//	Kill the current session, and show the login screen.
		session_destroy();
		header('Location: ../admin');
	}
}

?>